package com.chaber.containers.views;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Pos;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.util.Callback;

import com.chaber.containers.beans.ProductCategoryData;
import com.chaber.containers.common.JavaFXUtils;

public class ProductAddModRemView extends AbstractAddModRemView {

	// fields
	private TextField barCodeField;
	private TextField nameField;
	private TextField amountField;
	private Label cdField;
	private Label mdField;
	private ComboBox<ProductCategoryData> categoryComboBox;

	public ProductAddModRemView(String title) {
		super(title);
	}

	@Override
	public void activateEdition() {
		this.barCodeField.setDisable(false);
		this.nameField.setDisable(false);
		this.amountField.setDisable(false);
		this.categoryComboBox.setDisable(false);

		this.initModifyButton.setDisable(true);
		this.removeButton.setDisable(true);
		this.saveButton.setDisable(false);
	}

	@Override
	public void deactivateEdition() {
		this.barCodeField.setDisable(true);
		this.nameField.setDisable(true);
		this.amountField.setDisable(true);
		this.categoryComboBox.setDisable(true);
		
		this.initModifyButton.setDisable(false);
		this.removeButton.setDisable(false);
		this.saveButton.setDisable(true);
	}

	@Override
	public void clearFields() {
		this.errorsBox.getChildren().clear();
		
		this.barCodeField.setText(null);
		this.nameField.setText(null);
		this.amountField.setText(null);
		this.cdField.setText(null);
		this.mdField.setText(null);
	}

	@Override
	protected GridPane buildDetailsFields() {
		GridPane detailsFieldsGrid = new GridPane();
		detailsFieldsGrid.setVgap(20);
		detailsFieldsGrid.setHgap(10);

		Label barCodeLabel = new Label("Bar code:");
		this.barCodeField = new TextField();
		detailsFieldsGrid.add(barCodeLabel, 0, 0);
		detailsFieldsGrid.add(this.barCodeField, 1, 0);

		Label nameLabel = new Label("Name:");
		this.nameField = new TextField();
		detailsFieldsGrid.add(nameLabel, 0, 1);
		detailsFieldsGrid.add(this.nameField, 1, 1);

		Label amountLabel = new Label("Amount:");
		this.amountField = new TextField();
		this.amountField.textProperty().addListener(new ChangeListener<String>() {
			@Override
			public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
				if (newValue != null) {
					try {
						Integer.parseInt(newValue);
					} catch (NumberFormatException e) {
						amountField.setText(oldValue);
					}
				}
			}
		});
		detailsFieldsGrid.add(amountLabel, 0, 2);
		detailsFieldsGrid.add(this.amountField, 1, 2);

		Label cdLabel = new Label("Creation date:");
		this.cdField = new Label();
		detailsFieldsGrid.add(cdLabel, 0, 3);
		detailsFieldsGrid.add(this.cdField, 1, 3);

		Label mdLabel = new Label("Modification date:");
		this.mdField = new Label();
		detailsFieldsGrid.add(mdLabel, 0, 4);
		detailsFieldsGrid.add(this.mdField, 1, 4);
		
		Label categoryLabel = new Label("Category:");
		this.categoryComboBox = new ComboBox<>();
		this.categoryComboBox.setCellFactory(new Callback<ListView<ProductCategoryData>, ListCell<ProductCategoryData>>() {
			@Override
			public ListCell<ProductCategoryData> call(ListView<ProductCategoryData> arg0) {
				return JavaFXUtils.getListCell();
			}
		});
		this.categoryComboBox.setButtonCell(JavaFXUtils.getListCell());
		detailsFieldsGrid.add(categoryLabel, 0, 5);
		detailsFieldsGrid.add(this.categoryComboBox, 1, 5);
		
		detailsFieldsGrid.setAlignment(Pos.CENTER);

		return detailsFieldsGrid;
	}

	public TextField getBarCodeField() {
		return barCodeField;
	}

	public void setBarCodeField(TextField barCodeField) {
		this.barCodeField = barCodeField;
	}

	public TextField getNameField() {
		return nameField;
	}

	public void setNameField(TextField nameField) {
		this.nameField = nameField;
	}

	public TextField getAmountField() {
		return amountField;
	}

	public void setAmountField(TextField amountField) {
		this.amountField = amountField;
	}

	public Label getCdField() {
		return cdField;
	}

	public void setCdField(Label cdField) {
		this.cdField = cdField;
	}

	public Label getMdField() {
		return mdField;
	}

	public void setMdField(Label mdField) {
		this.mdField = mdField;
	}

	public ComboBox<ProductCategoryData> getCategoryComboBox() {
		return categoryComboBox;
	}

	public void setCategoryComboBox(ComboBox<ProductCategoryData> categoryComboBox) {
		this.categoryComboBox = categoryComboBox;
	}
}
